package com.example.baseballgame;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ScoreActivity extends Activity {
	
	private MyDbHelper helper;
	private TextView message;
	
	private ListView list;
	private SimpleCursorAdapter adapter;
	Cursor cursor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baseball_score);
		list = (ListView)findViewById(R.id.list);
		helper = new MyDbHelper(this, "baseballgameDB.sqlite", null, 1);
		
		adapter = new SimpleCursorAdapter(this, 
				android.R.layout.simple_list_item_2,//TextView가 2개 들어있음 
				cursor, 
				new String[] {"id","score"}, //컬럼명
				new int[]{android.R.id.text1, android.R.id.text2}, //simple_list_item_2 레이아웃에 TextView id가 text1, text2로 정의되었음
				0);
		list.setAdapter(adapter);
		
		refreshList();
		
	}
	
	@Override
	protected void onStop() {
		if(cursor != null && !cursor.isClosed()){
			cursor.close();
		}
		
		super.onStop();
	}
	
	private void refreshList(){
		
		SQLiteDatabase db = helper.getReadableDatabase();
		cursor = db.rawQuery("SELECT id, score FROM baseballgame", null);
		adapter.changeCursor(cursor);
		db.close();
	}

}
